﻿using Corina.DataContext.Master;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Corina.Webservice.Cydee
{
    /// <summary>
    /// Summary description for Manager
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Manager : WebserviceBase
    {

        /// <summary>
        /// 更新账户信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="name"></param>
        /// <param name="mobile"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        [WebMethod]
        public ResultInfo UpdateProfile(string account, string password, string name, string mobile, string email)
        {
            return GetInstance<ManagerMaster>().Update(account, password, name, mobile, email);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="passsword"></param>
        /// <param name="newpassword"></param>
        /// <returns></returns>
        [WebMethod]
        public ResultInfo ChangePassword(string account, string passsword, string newpassword)
        {
            return GetInstance<ManagerMaster>().UpdatePassword(account, passsword, newpassword);
        }

        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="account">管理员账户</param>
        /// <param name="password">管理员密码</param>
        /// <param name="newaccount">增加的管理员帐号</param>
        /// <param name="newpassword">增加的管理员密码</param>
        /// <param name="name">姓名</param>
        /// <param name="mobile">手机号</param>
        /// <param name="email">电子邮件</param>
        /// <param name="roleId">角色编号</param>
        /// <returns></returns>
        [WebMethod]
        public ResultInfo InsertManager(string account, string password, string newaccount, string newpassword, string name, string mobile, string email, int roleId)
        {
            if (account != "root")
            {
                return new ResultInfo(ResultValue.Failed);
            }
            ManagerInfo entity = new ManagerInfo()
            {
                Account = newaccount,
                Password = newpassword,
                Email = email,
                Mobile = mobile,
                RoleId = roleId,
                Name = name,
                AccountStatus = AccountStatusValue.Normal
            };

            return GetInstance<ManagerMaster>().Update(account, password, entity, EntityOperation.Insert);
        }

        [WebMethod]
        public ResultInfo UpdateStatus(string account, string password, int accountId, AccountStatusValue status)
        {
            if (account != "root")
            {
                return new ResultInfo(ResultValue.Failed);
            }
            return GetInstance<ManagerMaster>().UpdateStatus(account, password, accountId, status);
        }

        [WebMethod]
        public ResultInfo UpdateManagerRole(string account, string password, int accountId, int roleId)
        {
            if (account != "root")
            {
                return new ResultInfo(ResultValue.Failed);
            }
            return GetInstance<ManagerMaster>().UpdateRole(account, password, accountId, roleId);
        }

        [WebMethod]
        public ResultInfo GetAllManager(string account, string password)
        {
            if (account != "root")
            {
                return new ResultInfo(ResultValue.Failed);
            }
            return GetInstance<ManagerMaster>().GetAllItems(account, password);
        }

        [WebMethod]
        public ResultInfo GetManagerInfo(string account, string password, int id)
        {
            return GetInstance<ManagerMaster>().GetEntity(account, password, id);
        }

        [WebMethod]
        public ResultInfo GetAllRole(string account, string password)
        {
            if (account != "root")
            {
                return new ResultInfo(ResultValue.Failed);
            }

            return GetInstance<RoleMaster>().GetAllItems(account, password);
        }

        [WebMethod]
        public ResultInfo UpdateRole(string account, string password, string name, string code, int roleId)
        {
            RoleInfo entity = new RoleInfo()
            {
                Name = name,
                Code = code,
                Id = roleId
            };
            return GetInstance<RoleMaster>().Update(account, password, entity, EntityOperation.Update);
        }

        [WebMethod]
        public ResultInfo InsertRole(string account, string password, string name, string code)
        {
            RoleInfo entity = new RoleInfo()
            {
                Name = name,
                Code = code
            };
            return GetInstance<RoleMaster>().Update(account, password, entity, EntityOperation.Insert);
        }

        [WebMethod]
        public ResultInfo RemoveRole(string account, string password, int roleId)
        {
            RoleInfo entity = new RoleInfo()
            {
                Id = roleId
            };
            return GetInstance<RoleMaster>().Update(account, password, entity, EntityOperation.Delete);
        }
    }
}
